package com.telephone.purchase.dao;

import com.telephone.purchase.entity.User;
import com.telephone.purchase.model.UserInfoModifyRequest;
import com.telephone.purchase.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author fengyingeng
 * @date 2019/9/4 14:33
 * Description: 用户信息数据访问层
 */
@Repository
public class UserDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserDao.class);

    @Resource
    private UserMapper userMapper;

    /**
     * 更新用户信息
     * @param userInfoModifyRequest
     * @return int
     */
    public int flushUserInfo(UserInfoModifyRequest userInfoModifyRequest) {

        int updateResult = 0;

        try {
            updateResult = userMapper.updateInfo(userInfoModifyRequest);
        } catch (Exception e) {
            LOGGER.error("更新失败,错误信息{}.", e);
            e.printStackTrace();
        }
        return updateResult;
    }

    public int flushUserPassword(String phoneNumber, String newUserPassword){
        int updateResult = 0;
        try {
            updateResult = userMapper.updateUserPassword(phoneNumber, newUserPassword);
        } catch (Exception e) {
            LOGGER.error("更新失败,错误信息{}.", e);
            e.printStackTrace();
        }
        return updateResult;
    }

    public int flushPayPassword(String phoneNumber, String newPayPassword){
        int updateResult = 0;
        try {
            updateResult = userMapper.updatePayPassword(phoneNumber, newPayPassword);
        } catch (Exception e) {
            LOGGER.error("更新失败,错误信息{}.", e);
            e.printStackTrace();
        }
        return updateResult;
    }

    public User selectUserInfo(String phoneNumber){

        User user = null;
        try {
            user = userMapper.selectByPhoneNumber(phoneNumber);
        } catch (Exception e) {
            LOGGER.error("获取用户信息失败,错误信息{}.", e);
            e.printStackTrace();
        }
        return user;
    }
}
